/*
 * Created on Jun 8, 2004
 * by Mihai Postelnicu
 * email mihai@code.ro
 *
 *
 */
package ro.gateway.aida.search;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author Mihai Postelnicu email mihai@code.ro
 */
public class ResultsFormat {
	private long id;
	private String format;
	private String name;
	private boolean selected;
	private boolean primary;
	/**
	 * @return Returns the primary.
	 */
	public boolean isPrimary() {
		return primary;
	}
	/**
	 * @param primary The primary to set.
	 */
	public void setPrimary(boolean primary) {
		this.primary = primary;
	}
	/**
	 * @return Returns the selected.
	 */
	public boolean isSelected() {
		return selected;
	}
	/**
	 * @param selected The selected to set.
	 */
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	private long userId;
	private StringTokenizer st;
	
	public void initTokenizer() {
		st = new StringTokenizer(format, "#");
	}

	public ResultsFormat() {
		format = "";
		id=0;
		primary=false;
		selected=false;
		
	}

	public String getNextColumn() {
		if (st.hasMoreTokens())
			return st.nextToken();
		else
			return null;

	}

	public void addColumn(String cid) {
		if (format.indexOf(cid) != -1)
			return;
		format += "#" + cid;
	}

	public void removeColumn(String cid) {
		if (format.indexOf(cid) == -1)
			return;
		format.replaceFirst("#" + cid, "");
	}

	
	/**
	 * @return Returns the format.
	 */
	public String getFormat() {
		return format;
	}
	/**
	 * @param format
	 *            The format to set.
	 */
	public void setFormat(String format) {
		this.format = format;
	}
	/**
	 * @return Returns the id.
	 */
	public long getId() {
		return id;
	}
	/**
	 * @param id
	 *            The id to set.
	 */
	public void setId(long id) {
		this.id = id;
	}
	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name
	 *            The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return Returns the userId.
	 */
	public long getUserId() {
		return userId;
	}
	/**
	 * @param userId
	 *            The userId to set.
	 */
	public void setUserId(long userId) {
		this.userId = userId;
	}

	public static List getDefinedColumns() {
		try {
			Class c = ResultsFormat.class;
			ArrayList ret = new ArrayList();
			for (int i = 0; i < c.getFields().length; i++) {
				Field f = c.getFields()[i];
				if (f.getName().indexOf("COLUMN") != -1) {
					String col = (String) f.get(null);
					ret.add(col);
				}
			}
			return ret;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			return null;
		}
		
	}

	public static final String TITLE_COLUMN = "title";
	public static final String COUNTRY_COLUMN = "countries";
	public static final String COUNTY_COLUMN = "counties";
	public static final String REGION_COLUMN = "regions";
	public static final String CREGION_COLUMN = "cregions";
	public static final String TOWN_COLUMN = "towns";
	public static final String SECTOR_COLUMN = "sectors";
	public static final String FUNDING_ORG_COLUMN = "funding_orgs";
	public static final String IMPLEMENTING_ORG_COLUMN = "implementing_orgs";
	public static final String EXECUTING_ORG_COLUMN = "executing_orgs";
	public static final String FUNDING_AMOUNT_COLUMN = "funding_amount";
	public static final String FUNDING_AMOUNT_ORG_COLUMN = "funding_amount_org";
	public static final String FUNDING_AMOUNT_ORG_COMM_COLUMN = "funding_amount_org_comm";
	public static final String FUNDING_AMOUNT_ORG_DISB_COLUMN = "funding_amount_org_disb";
	public static final String FUNDING_STATUS_COLUMN = "funding_status";
	public static final String TYPES_ASSISTANCE_COLUMN = "types_assistance";	
	public static final String TERMS_ASSISTANCE_COLUMN = "terms_assistance";	
	public static final String PROPOSED_APPROVAL_DATE_COLUMN = "proposed_approval";
	public static final String ACTUAL_APPROVAL_DATE_COLUMN = "actual_approval";
	public static final String PROPOSED_STARTING_DATE_COLUMN = "proposed_starting";
	public static final String ACTUAL_STARTING_DATE_COLUMN = "actual_starting";
	public static final String STATUS_COLUMN = "status";
	public static final String PROJECT_ID_COLUMN = "project_ids";
	public static final String DESCRIPTION_COLUMN = "description";
	
	public static final String EXPANDABLE = 
		FUNDING_AMOUNT_COLUMN+FUNDING_AMOUNT_ORG_COLUMN+FUNDING_AMOUNT_ORG_COMM_COLUMN+FUNDING_AMOUNT_ORG_DISB_COLUMN;
	
	public static final String LIST = "formats";	

}